package com.hdoit.ares.web.policy.wechat;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.insu.model.FanhuaCar;
import com.hdoit.ares.logical.insu.model.FanhuaCarBusinessRiskKinds;
import com.hdoit.ares.logical.insu.model.ZhonganCar;
import com.hdoit.ares.logical.insu.service.IFanhuaAccidentService;
import com.hdoit.ares.logical.insu.service.IFanhuaCarBusinessRiskKindsService;
import com.hdoit.ares.logical.insu.service.IFanhuaCarService;
import com.hdoit.ares.logical.insu.service.IZhonganCarService;
import com.hdoit.ares.logical.policy.model.ListPolicy;
import com.hdoit.ares.logical.policy.service.IListPolicyService;
import com.hdoit.ares.selfcommon.enums.AresManageContainer;
import com.hdoit.ares.selfcommon.utils.PolicyInsuStateUtil;
import com.tennetcn.common.message.WechatLoginModel;
import com.tennetcn.component.wechat.BaseWechat;

@Controller
@RequestMapping(value="/wechat/policy/listPolicy/")
public class PolicyWechat extends BaseWechat{


	@Override
	public String getRoutePath() {
		return "areas/wechat/policy/listPolicy/";
	}
	
	private IFanhuaAccidentService fanhuaAccidentService;
	private IFanhuaCarService fanhuaCarService;
	private IFanhuaCarBusinessRiskKindsService fanhuaCarBusinessRiskKindsService;
	
	public IFanhuaAccidentService getAccidentInsuranceService() {
		return fanhuaAccidentService;
	}
	@Resource
	public void setAccidentInsuranceService(IFanhuaAccidentService fanhuaAccidentService) {
		this.fanhuaAccidentService = fanhuaAccidentService;
	}
	public IFanhuaCarService getFanhuaCarService() {
		return fanhuaCarService;
	}
	@Resource
	public void setFanhuaCarService(IFanhuaCarService fanhuaCarService) {
		this.fanhuaCarService = fanhuaCarService;
	}
	public IFanhuaCarBusinessRiskKindsService getBusinessRiskKindsService() {
		return fanhuaCarBusinessRiskKindsService;
	}
	@Resource
	public void setBusinessRiskKindsService(IFanhuaCarBusinessRiskKindsService fanhuaCarBusinessRiskKindsService) {
		this.fanhuaCarBusinessRiskKindsService = fanhuaCarBusinessRiskKindsService;
	}
	
	private IListPolicyService listPolicyService;
	public IListPolicyService getListPolicyService() {
		return listPolicyService;
	}
	@Resource
	public void setListPolicyService(IListPolicyService listPolicyService) {
		this.listPolicyService = listPolicyService;
	}
	
	private IZhonganCarService zhonganCarService;
	public IZhonganCarService getZhonganCarService() {
		return zhonganCarService;
	}
	@Resource
	public void setZhonganCarService(IZhonganCarService zhonganCarService) {
		this.zhonganCarService = zhonganCarService;
	}

	private final String policyViewName="policy";
	private final String policyDetailViewName="policyDetail";
	private final String carPolicyDetailViewName="carPolicyDetail";
	private final String policyListViewName="policyList";
	
	
	@RequestMapping(value = policyViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String policy(Model model){
		return view(policyViewName);
	}
	
	@RequestMapping(value = policyDetailViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String policyDetail(Model model,String oid,String signs){
		
		WechatLoginModel loginModel=wechatSession.getCurrentLogin();
		Customer customer=(Customer)loginModel.get(AresManageContainer.customer_account_info);
		String customerId = customer.getId();
		
		ListPolicy listPolicy = new ListPolicy();
		if(signs.equals("fanhuaAccident")){
			listPolicy = listPolicyService.queryFanhuaAccident(customerId,oid);
		}else if (signs.equals("zhonganAccident")){
			listPolicy = listPolicyService.queryZhonganAccident(customerId,oid);
		}
		model.addAttribute(listPolicy);
		return view(policyDetailViewName);
	}
	
	@RequestMapping(value = carPolicyDetailViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String carPolicyDetail(Model model,String oid,String signs){
		
		ListPolicy listPolicy = new ListPolicy();
		if(signs.equals("fanhuaCar")){
			listPolicy = listPolicyService.queryFanhuaCarDetail(oid);
			FanhuaCar fanhuaCar = fanhuaCarService.queryFanhuaCar(oid);
			System.err.println("listPolicy.getFanhuaCar().getId()++"+fanhuaCar.getId());
			
			List<FanhuaCarBusinessRiskKinds> fanhuaCarBusinessRiskKinds = fanhuaCarBusinessRiskKindsService.queryModelByOid(fanhuaCar.getId());
			
			model.addAttribute("fanhuaCarBusinessRiskKinds",fanhuaCarBusinessRiskKinds);
		}
		if(signs.equals("zhonganCar")){
			
			listPolicy = listPolicyService.queryZhonganCarDetail(oid);
			
			ZhonganCar zhongancar = zhonganCarService.queryModelByPolicyNo(listPolicy.getOid());
			String vehiclePolicyOrderNo = zhongancar.getVehiclePolicyOrderNo();
			System.err.println("vehiclePolicyOrderNo"+vehiclePolicyOrderNo);
			model.addAttribute("zhonganCar",zhongancar);
			
			if(!StringUtils.isEmpty(vehiclePolicyOrderNo)){
				ZhonganCar zhonganCarMain = zhonganCarService.queryModelByVehiclePolicyOrderNo(vehiclePolicyOrderNo,"2");
				model.addAttribute("zhonganCarMain",zhonganCarMain);
			}else {
				ZhonganCar zhonganCarElse = new ZhonganCar();
				model.addAttribute("zhonganCarElse",zhonganCarElse);
			}
		}
		
		model.addAttribute("listPolicy",listPolicy);
		return view(carPolicyDetailViewName);
	}
	
	@RequestMapping(value = policyListViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String policyList(Model model,String isCar,String insuState){
		
		WechatLoginModel loginModel=wechatSession.getCurrentLogin();		
		Customer customer=(Customer)loginModel.get(AresManageContainer.customer_account_info);
		model.addAttribute("customer",customer);
		List<ListPolicy> policys = new ArrayList<ListPolicy>();
		
		String msg = "";
		//车险筛选
		if("carInsu".equals(isCar)){
			System.err.println("进入车险");
			List<ListPolicy> carPolicys = listPolicyService.queryAllCarPolicy(customer.getId());
			for (ListPolicy carPolicy : carPolicys) {
				PolicyInsuStateUtil.changeState(carPolicy);
			}
			
			if("all".equals(insuState)){
				for (ListPolicy listPolicy : carPolicys) {
					policys.add(listPolicy);
				}	
				msg = "您还没有任何车险保单哦...";
				
			}else if ("ready".equals(insuState)) {
				
				for (ListPolicy listPolicy : carPolicys) {
					if(listPolicy.getInsuState().equals("ready")){
						policys.add(listPolicy);
					}					
				}
				msg = "您还没有任何待承保 的车险哦...";
				
			}else if ("yes".equals(insuState)) {
				
				for (ListPolicy listPolicy : carPolicys) {
					if(listPolicy.getInsuState().equals("yes")){
						policys.add(listPolicy);
					}	
					System.out.println("承保中 的车险："+policys.size());
				}
				msg = "您还没有任何承保中 的车险哦...";
				
			}else if ("no".equals(insuState)) {
				
				for (ListPolicy listPolicy : carPolicys) {
					if(listPolicy.getInsuState().equals("no")){
						policys.add(listPolicy);
					}					
				}
				msg = "您还没有任何已失效 的车险哦...";				
			}
		}
		
		//非车险筛选
		if("notCarInsu".equals(isCar)){
			System.err.println("进入非车险");
			List<ListPolicy> accidentPolicys = listPolicyService.queryAllAccidentPolicy(customer.getId());
			
			for (ListPolicy accidentPolicy : accidentPolicys) {
				PolicyInsuStateUtil.changeState(accidentPolicy);
			}
			

			if("all".equals(insuState)){

				for (ListPolicy listPolicy : accidentPolicys) {
					policys.add(listPolicy);
				}			
				msg = "您还没有任何非车险保单哦...";
				
			}else if ("ready".equals(insuState)) {
				
				for (ListPolicy listPolicy : accidentPolicys) {
					if(listPolicy.getInsuState().equals("ready")){
						policys.add(listPolicy);
					}					
				}
				msg = "您还没有任何待承保 的非车险哦...";
				
			}else if ("yes".equals(insuState)) {
				
				for (ListPolicy listPolicy : accidentPolicys) {
					if(listPolicy.getInsuState().equals("yes")){
						policys.add(listPolicy);
					}	
					System.out.println("承保中 的非车险："+policys.size());
				} 
				msg = "您还没有任何承保中 的非车险哦...";
				
			}else if ("no".equals(insuState)) {
				
				for (ListPolicy listPolicy : accidentPolicys) {
					if(listPolicy.getInsuState().equals("no")){
						policys.add(listPolicy);
					}					
				}
				msg = "您还没有任何已失效 的非车险哦...";
				
			}else if ("fail".equals(insuState)) {
				for (ListPolicy listPolicy : accidentPolicys) {
					if(listPolicy.getInsuState().equals("fail")){
						policys.add(listPolicy);
						System.err.println("asdsadsa"+listPolicy.getStartDate());
					}	
					System.out.println("失败 的非车险："+policys.size());
					
				}
				msg = "您还没有任何承保失败 的非车险哦...";
			}	
		}
		model.addAttribute("policys",policys);				
		model.addAttribute("msg",msg);
		return view(policyListViewName);
	}
	
}
